<?php

/**
 * Description of sort_map
 * 
 * @author 叶风
 * Qq:    355007778
 * Email: ye.fn@163.com
 */

namespace m;

class sort_map extends \control
{
    function get($id)
    {
        $t = array('map'=>array('as'=>$this->table),
                    'sort'=>array( 'join'=>'LEFT JOIN', 'on'=>'map.sort_id=sort.id', 'as'=>$this->table_sort));
        return $this->db->get($t, 'map.sort_id id, sort.name', 'map.data_id='.$id);
    }
    function del($id, $sid='')
    {
        $w = "data_id IN($id)";
        if ($sid)
            $w .= " AND sort_id IN($sid)";
        $this->db->del($this->table, $w);
    }
    function save($id, $a)
    {
        foreach ($a as $sid)
            $this->db->save($this->table, array('data_id'=>$id, 'sort_id'=>intval($sid)));
    }
    
    //tree
    function tree($pid)
    {
        $t = array('map'=>array('as'=>$this->table),
                    'sort'=>array( 'join'=>'RIGHT JOIN', 'on'=>'map.sort_id=sort.id', 'as'=>$this->table_sort),);
        return $this->db->group('sort.id')->get($t, 'sort.id, sort.name, count(map.data_id) iNum, 1 subNum', 'sort.parent_id='.$pid);
    }
}
